<?php

namespace App\Http\Controllers\admin\api;

use App\Http\Controllers\admin\service\AppService;
use App\Http\Controllers\admin\validate\App;
use App\Http\Controllers\utils\ToolUtil;
use Illuminate\Http\Request;
use App\Http\Controllers\admin\model\AdminModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

/**
 * 阿里云oss
 * Class AliossApi
 * @package App\Http\Controllers\admin\api
 */
class AliossApi
{
    /**
     * 获取上传apk到oss所需信息(添加)
     * @param Request $request
     * @return false|string
     */
    public function getOssApkInfoAdd(Request $request){

        $param = $request->all();
        $data = App::queryBatchName($param);
        if($data){
            return ToolUtil::returnJson(401,$data[0]);
        }

        // $admin = AdminModel::queryLeftVip(['a.id'=>$param['userId']]);
        $admin = DB::table('t_admin as a')
            ->leftJoin('t_vip as v','a.vip_id','=','v.id')
            ->where(['a.id'=>$param['userId']])
            ->first(['a.apk_num','v.vip_size','v.vip_num','v.upgrade']);
        if($admin){
            if($admin['apk_num']>=($admin['vip_num']+$admin['upgrade'])){
                return ToolUtil::returnJson(301,'文件上传达到限制，请联系管理员');
            }
        }else{
            return ToolUtil::returnJson(404,'没找到用户');
        }

        $max_size = $admin['vip_size']*1024*1024;

        $data = $this->getOssInfo($max_size);

        return ToolUtil::returnJson(200,'成功',$data);
    }

    /**
     * 获取上传apk到oss所需信息(编辑)
     * @param Request $request
     * @return false|string
     */
    public function getOssApkInfoEdit(Request $request){
        $param = $request->all();
        $data = App::queryBatchName($param);
        if($data){
            return ToolUtil::returnJson(401,$data[0]);
        }

        // $admin = AdminModel::queryLeftVip(['a.id'=>$param['userId']]);
        $admin = DB::table('t_admin as a')
            ->leftJoin('t_vip as v','a.vip_id','=','v.id')
            ->where(['a.id'=>$param['userId']])
            ->first(['a.apk_num','v.vip_size','v.vip_num','v.upgrade']);
        if(empty($admin)){
            return ToolUtil::returnJson(404,'没找到用户');
        }

        $max_size = $admin['vip_size']*1024*1024;

        $data = $this->getOssInfo($max_size);

        return ToolUtil::returnJson(200,'成功',$data);
    }

    /**
     * 获取上传图片到oss所需信息
     * @param Request $request
     * @return false|string
     */
    public function getOssImgInfo(Request $request){
        $param = $request->all();
        $data = App::queryBatchName($param);
        if($data){
            return ToolUtil::returnJson(401,$data[0]);
        }
        $max_size = 1048576;

        $data = $this->getOssInfo($max_size);

        return ToolUtil::returnJson(200,'成功',$data);
    }


    /**
     * 获取oss信息
     * @param num $max_size
     * @return array
     */
    private function getOssInfo($max_size){

        $date = date('Y-m-d',time());
        $expire = time()+100;
        $time = date('H:i:s',$expire);
        $time_str = $date.'T'.$time.'.000Z';

        $accessid = env('OSS_ACCESS_KEY_ID');
        $accesskey = env('OSS_ACCESS_KEY_SECRET');
        $host = env('OSS_HOST');

        $policy = '{"expiration": "'.$time_str.'","conditions": [["content-length-range", 10, '.$max_size.'],["starts-with", "$key", "homework"]]}';
        $policy = base64_encode($policy);

        $signature = base64_encode(hash_hmac("sha1", $policy, $accesskey, true));
        
        $data = [
            'max_size'=>$max_size,
            'host'=>$host,
            'accessid'=>$accessid,
            'expire'=>$expire,
            'policyBase64'=>$policy,
            'signature'=>$signature
        ];

        return $data;
    }


}
